package com.web.exiaodao.remote.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.result.XaResult;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.exiaodao.remote.service.ApiTravelTableService;
import com.web.exiaodao.remote.vo.TravelTableVo;

/**
 * @Title: ApiTravelTableController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 旅游表(游学)信息接口
 * @author zhanglin
 * @date 2016-11-02 15:02:37
 * @version V1.0
 */
@Api(value = "TravelTable", description = "旅游表(游学)接口", position = 10)
@Controller
@RequestMapping("/api/travelTable")
public class ApiTravelTableController extends BaseController {

	private static final Logger LOGGER = Logger.getLogger(ApiTravelTableController.class);

	@Autowired
	private ApiTravelTableService travelTableService;
	
	/**
	 * @Title: findTravelTableList
	 * @Description: 查询所有旅游表(游学)信息
	 * @return    
	 */
	@ApiOperation(value="查询所有旅游表(游学)",notes="查询所有旅游表(游学)信息,当返回的code=1时，取出object进行显示,存放为数组对象")
	@ResponseBody
	@RequestMapping(value="findTravelTableList",method=RequestMethod.POST)
	public XaResult<Page<TravelTableVo>> findTravelTableList(
		@ApiParam("纬度,字段名:lat,必填") @RequestParam(value = "lat2", required=false) Double lat2,   
		@ApiParam("经度,字段名:lng,必填") @RequestParam(value = "lng2", required=false) Double lng2,
		@ApiParam("项目种类,1：国内 2:国外；（默认国内1）,字段名:travelItemType,默认1") @RequestParam(defaultValue = "1") Integer travelItemType,
		@ApiParam("页号,字段名:nextPage,默认0,从第0页开始") @RequestParam(defaultValue = "0") Integer nextPage,
		@ApiParam("页长,字段名:pageSize,默认10") @RequestParam(defaultValue = "10") Integer pageSize
	) throws BusinessException{
        XaResult<Page<TravelTableVo>> xr = new  XaResult<Page<TravelTableVo>>();
		Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, "[{property:'sort',direction:'ASC'},{property:'clickCont',direction:'DESC'},{property:'createTime',direction:'DESC'}]");
		try{
			TravelTableVo travelTableVo = new TravelTableVo();
			travelTableVo.setTravelItemType(travelItemType);
			return travelTableService.findTravelTableList(lat2,lng2,pageable,travelTableVo);
		}catch(Exception e){
			if (e instanceof BusinessException) {
				xr.error(e.getMessage(), true);
			} else  {
				xr.error(XaConstant.Message.error, true);
			}
			LOGGER.error(XaConstant.Message.error, new Throwable(e));
		}
		return xr;
	}

	
	/**
	 * @Title: findTravelTableById
	 * @Description: 根据ID查找单条实体
	 * //@param modelId
	 * @return    
	 */
	@ApiOperation(value="旅游表(游学)详情",notes="查询旅游表(游学)详细信息,当返回的code=1时，取出object进行显示,存放为单体对象")
	@ResponseBody
	@RequestMapping(value="findTravelTableById",method=RequestMethod.POST)
	public XaResult<List<TravelTableVo>> findTravelTableById(
		@ApiParam("用户ID,字段名:userId,必填") @RequestParam(value = "userId") Long userId,
		@ApiParam("项目ID,字段名:id,必填") @RequestParam(value = "id") Long id  
	) throws BusinessException{
		XaResult<List<TravelTableVo>> xr = new XaResult<List<TravelTableVo>>();
		try{
			TravelTableVo travelTableVo = new TravelTableVo();
			travelTableVo.setId(id);
			return travelTableService.findTravelTableById(userId,travelTableVo);
		}catch(Exception e){
			if (e instanceof BusinessException) {
						 xr.error(e.getMessage());
			} else  {
				 xr.error(XaConstant.Message.error);
			}
			 LOGGER.error(XaConstant.Message.error, new Throwable(e));
		}
		return xr;
	}
	
}

